﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WOWMultiPlay
{
    public partial class Form_Main : Form
    {
        public Form_Main()
        {
            InitializeComponent();
        }

        HotkeyWndProcHost hotkeyWndProcHost = new HotkeyWndProcHost();

        private void Form_Main_Load(object sender, EventArgs e)
        {
            LoadUserSettings();
            CreateProfileButtons(Settings.Profile.mpButtons);
        }

        private void Form_Main_FormClosing(object sender, FormClosingEventArgs e)
        {
            ClearAllHotkeys();
        }

        private void ClearAllHotkeys()
        {
            foreach (mpButton mpbtn in Settings.Profile.mpButtons)
            {
                if (mpbtn.Hotkey != null)
                {
                    mpbtn.Hotkey.UnregisterGlobalHotKey();
                }
            }
        }

        private void LoadUserSettings()
        {
            this.Opacity = Settings.UserSettings.AppOpacity / 100;
            this.Height = Settings.UserSettings.WindowHeight;
            this.Width = Settings.UserSettings.WindowWidth;
        }

        List<Button> ProfileWinFormButtons;

        private void LoadProfileButtons()
        {
            ClearAllHotkeys();

            Settings.Profile = new mpProfile(Settings.Profile.ProfilePath);

            ClearProfileButtons();
            CreateProfileButtons(Settings.Profile.mpButtons);
        }

        private void ClearProfileButtons()
        {
            ButtonsLayoutPanel.Controls.Clear();
            ButtonsLayoutPanel.ColumnCount = 1;
            ButtonsLayoutPanel.RowCount = 1;

            foreach (Button btn in ProfileWinFormButtons)
            {
                ((mpButton)btn.Tag).StopSendKeyArray();
                btn.Dispose();
            }
        }

        private void CreateProfileButtons(List<mpButton> ProfileButtons)
        {
            CreateButtonsLayoutPanel(ProfileButtons);

            ProfileWinFormButtons = new List<Button>(ProfileButtons.Count);

            int AddedButtonCount = 0;
            int AddingCol = 0;
            int AddingRow = 0;

            if (ProfileButtons.Count != 0)
            {
                for (int i = 0; i < ProfileButtons.Count; i++)
                {
                    ProfileWinFormButtons.Add(new Button());
                    ProfileWinFormButtons[i].Dock = DockStyle.Fill;
                    ProfileWinFormButtons[i].Text = ProfileButtons[i].ButtonName;
                    ProfileWinFormButtons[i].Tag = ProfileButtons[i];
                    ProfileWinFormButtons[i].Click += new System.EventHandler(this.ProfileWinFormButton_Click);
                    ProfileWinFormButtons[i].UseVisualStyleBackColor = true;

                    if (ProfileButtons[i].Hotkey != null)
                    {
                        ProfileButtons[i].Hotkey.Handle = hotkeyWndProcHost.Handle;
                        ProfileButtons[i].Hotkey.WinFormButton = ProfileWinFormButtons[i];
                        try
                        {
                            ProfileButtons[i].Hotkey.RegisterGlobalHotKey();
                        }
                        catch (Exception e)
                        {
                            MessageBox.Show(e.Message);
                        }
                        ProfileButtons[i].Hotkey.OnReceivedHotkey += new mpHotkey.ReceivedHotkey(Hotkey_OnReceivedHotkey);
                    }
                    
                    ButtonsLayoutPanel.Controls.Add(this.ProfileWinFormButtons[i], AddingCol, AddingRow);
                    AddedButtonCount++;
                    AddingCol++;

                    if (AddingCol > ButtonsLayoutPanel.ColumnCount - 1)
                    {
                        AddingCol = 0;
                        AddingRow++;
                    }
                }
            }
        }

        private void Hotkey_OnReceivedHotkey(object sender, Button btn)
        {
            ProfileWinFormButton_Click(btn, null);
        }

        private void CreateButtonsLayoutPanel(List<mpButton> ProfileButtons)
        {
            int ButtonsLayoutPanelLines = ProfileButtons.Count % Settings.UserSettings.MaxButtonCountPerLine == 0
                           ? ProfileButtons.Count / Settings.UserSettings.MaxButtonCountPerLine
                           : ProfileButtons.Count / Settings.UserSettings.MaxButtonCountPerLine + 1;

            if (ProfileButtons.Count < Settings.UserSettings.MaxButtonCountPerLine)
            {
                ButtonsLayoutPanel.ColumnCount = ProfileButtons.Count;
                ButtonsLayoutPanel.RowCount = 1;
            }
            else
            {
                ButtonsLayoutPanel.ColumnCount = Settings.UserSettings.MaxButtonCountPerLine;
                ButtonsLayoutPanel.RowCount = ButtonsLayoutPanelLines;
            }

            ButtonsLayoutPanel.ColumnStyles.Clear();
            Single ColPercent = ButtonsLayoutPanel.ColumnCount != 0 ? 100 / ButtonsLayoutPanel.ColumnCount : 1;

            for (int i = 0; i < ButtonsLayoutPanel.ColumnCount; i++)
            {
                ButtonsLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, ColPercent));
            }

            ButtonsLayoutPanel.RowStyles.Clear();
            Single RowPercent = 100 / ButtonsLayoutPanel.RowCount;

            for (int i = 0; i < ButtonsLayoutPanel.RowCount; i++)
            {
                ButtonsLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, RowPercent));
            }
        }

        private void ProfileWinFormButton_Click(object sender, EventArgs e)
        {
            Button ProfileWinFormButton = (Button)sender;
            mpButton ProfileButton = (mpButton)ProfileWinFormButton.Tag;
            ProfileButton.WinFormButton = ProfileWinFormButton;
            ProfileButton.OnSendKeyArrayFinished += new mpButton.SendKeyArrayFinished(ProfileButton_OnSendKeyArrayFinished);

            if (ProfileButton.SendKeysThreads.Count == 0)
            {
                Settings.NormalButtonFontColor = ProfileWinFormButton.ForeColor;
                ProfileWinFormButton.ForeColor = Settings.RunningButtonFontColor;
                foreach (IntPtr instance in Settings.Instances)
                {
                    ProfileButton.SendKeyArray(instance);
                }
            }
            else
            {
                ProfileButton.StopSendKeyArray();

                ProfileWinFormButton.ForeColor = Settings.NormalButtonFontColor;
                ProfileButton.SendKeysThreads.Clear();
            }
        }

        private void ProfileButton_OnSendKeyArrayFinished(object sender, EventArgs e)
        {
            mpButton ProfileButton = (mpButton)sender;
            ProfileButton.WinFormButton.ForeColor = Settings.NormalButtonFontColor;
            ProfileButton.SendKeysThreads.Clear();
        }

        private void ToolStripMenuItem_Options_Click(object sender, EventArgs e)
        {
            Form_Options _Form_Options = new Form_Options();
            _Form_Options.ShowDialog();

            LoadUserSettings();
            LoadProfileButtons();
        }

        private void ToolStripMenuItem_EditButtons_Click(object sender, EventArgs e)
        {
            Form_ButtonOptions _Form_ButtonOptions = new Form_ButtonOptions();
            _Form_ButtonOptions.ShowDialog();

            LoadProfileButtons();
        }

        private void Form_Main_ResizeEnd(object sender, EventArgs e)
        {
            Settings.UserSettings.WindowHeight = this.Height;
            Settings.UserSettings.WindowWidth = this.Width;

            Serializer<UserSettings>.Serialize(Settings.UserSettings, Settings.SettingsFilePath);
        }
    }
}